/*
    
    Copyright (C) 2011  Antoine

 	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
   	the Free Software Foundation; either version 3 of the License, or
   	(at your option) any later version.

   	This program is distributed in the hope that it will be useful,
   	but WITHOUT ANY WARRANTY; without even the implied warranty of
   	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   	GNU General Public License for more details.

   	You should have received a copy of the GNU General Public License
   	along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package subs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.table.DefaultTableModel;

public class Sub {

	public static File			file;
	public static MainView		vue;
	private ArrayList<String[]>	array_sub	= new ArrayList<String[]>();
	private String				str			= "";
	Date						date_debut;
	Date						date_fin;
	long						duree_long;
	SimpleDateFormat			formatter	= new SimpleDateFormat("HH:mm:ss,SSS");

	public Sub(File file, MainView vue) {
		Sub.file = file;
		this.vue = vue;
	}

	public void displayBoth() {

		try {

			// Lecture fichier
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);

			// Lecture premiere ligne
			String line = br.readLine();

			// ligne en cours
			boolean line_id = true, line_date_debut = false, line_text = false;

			// tableau du sous titre
			String[] sub_tab = new String[4];

			// texte du sous titre
			String text_to_array = "";

			// Split du temps
			String[] temp; // Split de la string
			String delimiter = " --> "; // delimiter pour parser

			// Lecture ligne par ligne du fichier choisi
			while (line != null) {

				if (line.trim().length() == 0) {
					line_text = false;
					line_id = true;
					sub_tab[3] = text_to_array;
					text_to_array = "";
					array_sub.add(sub_tab.clone());
				}
				else if (line_text) {
					text_to_array += line + "\n";
				}
				else if (line_date_debut) {
					temp = line.split(delimiter);
					sub_tab[1] = temp[0];
					sub_tab[2] = temp[1];
					line_date_debut = false;
					line_text = true;
				}
				else if (line_is_int(line) && line_id) {
					sub_tab[0] = line;
					line_id = false;
					line_date_debut = true;
				}

				// Pour editorPane
				str += line + "\n";

				line = br.readLine();

			}

			DefaultTableModel model = (DefaultTableModel) vue.table.getModel();
			model.setRowCount(0);

			// Remplissage JTable table
			for (String[] p : array_sub) {
				model.addRow(p);
			}

			vue.editorPane.setText(str);
			vue.editorPane.setCaretPosition(0);

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updatePane() {
		String table_string = "";
	
		for(int i=0; i < vue.table.getRowCount(); i++){
			table_string += (String) vue.table.getValueAt(i, 0) + "\n";
			table_string += (String) vue.table.getValueAt(i, 1);
			table_string += " --> " + (String) vue.table.getValueAt(i, 2) + "\n";
			table_string += (String) vue.table.getValueAt(i, 3) + "\n";
		}
		
		vue.editorPane.setText(table_string);
		vue.editorPane.setCaretPosition(0);
		
	}

	public boolean line_is_int(String str) {
		char[] all = str.toCharArray();
		for (int i = 0; i < all.length; i++) {
			if (!Character.isDigit(all[i])) {
				return false;
			}
		}
		return true;
	}

	public void updateEditor() {
		int rowIndex = vue.table.getSelectedRow();

		String value_date_debut = (String) vue.table.getValueAt(rowIndex, 1);
		String value_date_fin = (String) vue.table.getValueAt(rowIndex, 2);
		String value_texte_fin = (String) vue.table.getValueAt(rowIndex, 3);

		// Durée
		try {
			date_debut = formatter.parse(value_date_debut);
			date_fin = formatter.parse(value_date_fin);
			duree_long = date_fin.getTime() - date_debut.getTime();
		}
		catch (Exception ex) {

		}
		// Fin durée

		vue.debut_sub.setText(value_date_debut);
		vue.editor_texte.setText(value_texte_fin);
		vue.duree_sub.setValue(duree_long);
	}

}
